#pragma once

#include <cassert>
#include <iostream>
#include <vector>
#include <cstddef> // 用于size_t类型
#include <mutex>

template <typename T>
class CircularBuffer {
private:
    std::vector<T> data;
    int head;
    int tail;
    int capacity;
    std::mutex mtx;
public:
    CircularBuffer(int size) : data(size), head(0), tail(0), capacity(size) {}
 
    void push(const T& value) {
        // if ((tail + 1) % capacity == head) {
        //     return ; // 队列已满
        // }
        if(isFull())
            return ;
        std::unique_lock<std::mutex> lck(mtx);
        data[tail] = value;
        tail = (tail + 1) % capacity;
        // return true;
    }
 
    T pop() {
        if (head == tail) {
            // return ; // 队列为空
            throw std::runtime_error("Empty queue!!!!");
        }
        T value;
        std::unique_lock<std::mutex> lck(mtx);
        value = data[head];
        head = (head + 1) % capacity;
        // return true;
        return value;
    }
    bool isEmpty() const{
        if (head == tail) 
            return true; // 队列为空
        else 
            return false;
    }
    bool isFull() const{
        if ((tail + 1) % capacity == head) 
            return true; // 队列已满
        else
            return false;
        
    }
    size_t size() const {
        return (tail - head + capacity) % capacity;
     }
    // 其他成员函数...
};
 